package com.shop2cn.iapi.coupon.testcase.old;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.coupon.parameter.QueryReceivableSellerBatchBean;
import com.shop2cn.iapi.coupon.parameter.UserReceiveCouponBean;
import com.shop2cn.iapi.coupon.parameter.UserReceiveResultInfo;
import com.shop2cn.iapi.coupon.parameter.batchInfo;
import com.shop2cn.iapi.coupon.parameter.batchInfos;
import com.shop2cn.iapi.coupon.service.QueryReceivableSellerBatchCall;
import com.shop2cn.iapi.coupon.service.QueryReceivableSellerBatchCall.batchDtos;
import com.shop2cn.iapi.coupon.service.UserReceiveCouponCall;
import com.shop2cn.iapi.couponmanage.parameter.CreateCouponBean;
import com.shop2cn.iapi.couponmanage.parameter.SellerCreateBatchBean;
import com.shop2cn.iapi.couponmanage.service.CreateCouponCall;
import com.shop2cn.iapi.couponmanage.service.SellerCreateBatchCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.CouponShop2cnMCallService;
import com.ymttest.database.model.coupon;
import com.ymttest.database.model.couponbatch;
import com.ymttest.database.sqlwapper.couponWapper;
import com.ymttest.database.sqlwapper.couponbatchWapper;
import com.ymttest.utils.TestDataManager;
/**
*获取可领的买手优惠券批次列表
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_QueryReceivableSellerBatch{
	private static QueryReceivableSellerBatchBean bean;
	private static QueryReceivableSellerBatchCall call;
	private static UserReceiveCouponBean userreceivecouponBean;
	private static UserReceiveCouponCall userreceivecouponCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取可领的买手优惠券批次列表-从内部缓存中取，需要再次根据领取开始结束时间筛选");
	}

	@Before
	public void caseUp() {
		bean=new QueryReceivableSellerBatchBean();
		call=new QueryReceivableSellerBatchCall();
		userreceivecouponBean = new UserReceiveCouponBean();
		userreceivecouponCall = new UserReceiveCouponCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Ts_QueryReceivableSellerBatch_001() {
		Logger.start(true,"正例-获取可领的买手优惠券批次列表-普通商家优惠券");
		try {
			//int userId=TestDataManager.getRandomIntNum(5);
			int userId=25356;
			CouponShop2cnMCallService couponCallService = new CouponShop2cnMCallService();
			// 商家优惠券
			SellerCreateBatchBean sellerCreateBatchBean = couponCallService.setDefaultSellerCreateBatchBean();	
			sellerCreateBatchBean.setSellerId(userId);
			SellerCreateBatchCall sellerCreateBatchCall = couponCallService.SellerCreateBatch(sellerCreateBatchBean);

			userreceivecouponBean = couponCallService.setUserReceiveCouponBean(sellerCreateBatchCall.getBatchCode());
			userreceivecouponBean.setUserId(userId);			
			userreceivecouponCall=couponCallService.receiveCouponInfo(sellerCreateBatchCall.getBatchCode(),userId);
			
			bean.setSellerId(sellerCreateBatchBean.getSellerId());
			call.setData(bean);
			call.callService();
			
			VerifyReturn();
			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Ts_QueryReceivableSellerBatch_002() {
		Logger.start(true,"正例-获取可领的买手优惠券批次列表-买手活动优惠券");
		try {
			int userId=TestDataManager.getRandomIntNum(5);
			//int userId=25356;
			CouponShop2cnMCallService couponCallService = new CouponShop2cnMCallService();
			// 商家优惠券
			SellerCreateBatchBean sellerCreateBatchBean = couponCallService.setDefaultSellerCreateBatch_ActivityBean();	
			
			SellerCreateBatchCall sellerCreateBatchCall = couponCallService.SellerCreateBatch(sellerCreateBatchBean);

			userreceivecouponBean = couponCallService.setUserReceiveCouponBean(sellerCreateBatchCall.getBatchCode());

			userreceivecouponBean.setUserId(userId);		
			userreceivecouponCall=couponCallService.receiveCouponInfo(sellerCreateBatchCall.getBatchCode(),userId);
			bean.setSellerId(sellerCreateBatchBean.getSellerId());
			call.setData(bean);
			call.callService();
			
			VerifyReturn();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	@Ignore
	public void Ts_QueryReceivableSellerBatch_003() {
		Logger.start(true,"正例-获取可领的买手优惠券批次列表-普通优惠券，now<缓存中的领取开始时间 && now<缓存中的领取结束时间-修改领取开始结束时间无效");
		try {
			int userId=TestDataManager.getRandomIntNum(5);
			CouponShop2cnMCallService couponCallService = new CouponShop2cnMCallService();
			// 商家优惠券
			SellerCreateBatchBean sellerCreateBatchBean = couponCallService.setDefaultSellerCreateBatchBean();	
			
			Date dNow = new Date(); // 当前时间
	        Date dBefore = new Date();
	        Date dAfter = new Date();

	        Calendar calendar1 = Calendar.getInstance(); 
	        calendar1.setTime(dNow);// 把当前时间赋给日历
	        calendar1.add(Calendar.DAY_OF_MONTH, -1); 
	        dBefore = calendar1.getTime(); // 

	        Calendar calendar2 = Calendar.getInstance(); 
	        calendar2.setTime(dNow);// 把当前时间赋给日历
	        calendar2.add(Calendar.MILLISECOND, 4000); 
	        dAfter = calendar2.getTime(); 
	        
	        sellerCreateBatchBean.setAcquireStartTime(dBefore);
	        sellerCreateBatchBean.setAcquireEndTime(dAfter);
	        
	        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	        Logger.comment("###getAcquireStartTime:"+formatter.format(sellerCreateBatchBean.getAcquireStartTime()));
	        Logger.comment("###setAcquireEndTime:"+formatter.format(sellerCreateBatchBean.getAcquireEndTime()));
	        
			SellerCreateBatchCall sellerCreateBatchCall = couponCallService.SellerCreateBatch(sellerCreateBatchBean);
	        
			userreceivecouponBean = couponCallService.setUserReceiveCouponBean(sellerCreateBatchCall.getBatchCode());		
			userreceivecouponCall=couponCallService.receiveCouponInfo(sellerCreateBatchCall.getBatchCode(),userId);
			Thread.sleep(10000);
			bean.setSellerId(sellerCreateBatchBean.getSellerId());
			call.setData(bean);
			call.callService();
			
			batchDtos userReceiveResult = call.getbatchDtosResult();
			Logger.verifyEquals(0, userReceiveResult.getBatchDtos().size(),"验证返回为空");
			
			Logger.verifyEquals(true, call.getMessage().contains("操作成功！"),"验证返回Message");
			Logger.verifyEquals("0", call.getCode(),"验证返回StatusCode");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Ts_QueryReceivableSellerBatch_004() {
		Logger.start(true,"正例-获取可领的买手优惠券批次列表-买手活动优惠券，now<缓存中的领取开始时间 && now<缓存中的领取结束时间");
		try {
			int userId=TestDataManager.getRandomIntNum(5);
			//int userId=25356;
			CouponShop2cnMCallService couponCallService = new CouponShop2cnMCallService();
			// 商家优惠券
			SellerCreateBatchBean sellerCreateBatchBean = couponCallService.setDefaultSellerCreateBatch_ActivityBean();	
			Date dNow = new Date(); // 当前时间
	        Date dBefore = new Date();
	        Date dAfter = new Date();

	        Calendar calendar1 = Calendar.getInstance(); 
	        calendar1.setTime(dNow);// 把当前时间赋给日历
	        calendar1.add(Calendar.DAY_OF_MONTH, -1); 
	        dBefore = calendar1.getTime(); // 

	        Calendar calendar2 = Calendar.getInstance(); 
	        calendar2.setTime(dNow);// 把当前时间赋给日历
	        calendar2.add(Calendar.MILLISECOND, 4000); 
	        dAfter = calendar2.getTime(); 
	        
	        sellerCreateBatchBean.setAcquireStartTime(dBefore);
	        sellerCreateBatchBean.setAcquireEndTime(dAfter);
			SellerCreateBatchCall sellerCreateBatchCall = couponCallService.SellerCreateBatch(sellerCreateBatchBean);

			userreceivecouponBean = couponCallService.setUserReceiveCouponBean(sellerCreateBatchCall.getBatchCode());

			userreceivecouponBean.setUserId(userId);		
			userreceivecouponCall=couponCallService.receiveCouponInfo(sellerCreateBatchCall.getBatchCode(),userId);
			Thread.sleep(10000);
			bean.setSellerId(sellerCreateBatchBean.getSellerId());
			call.setData(bean);
			call.callService();
			
			batchDtos userReceiveResult = call.getbatchDtosResult();
			Logger.verifyEquals(0, userReceiveResult.getBatchDtos().size(),"验证返回为空");
			
			Logger.verifyEquals(true, call.getMessage().contains("操作成功！"),"验证返回Message");
			Logger.verifyEquals("0", call.getCode(),"验证返回StatusCode");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
    private void VerifyReturn()
    {
    	batchDtos userReceiveResult = call.getbatchDtosResult();
    	if(userReceiveResult.getBatchDtos().size()>0)
    	{
    		int count=0;
    	  for(batchInfo info:userReceiveResult.getBatchDtos())
    	   {
    		    count++;
    		    Logger.comment("验证batchId:"+info.getBatchId());
				// 验证第"+count+"条返回
			//	Logger.verifyEquals(userreceivecouponBean.getUserId(), info.getSellerId(), "验证第"+count+"条返回SellerId");
			//	Logger.verifyEquals(userreceivecouponBean.getBatchCode(), info.getBatchCode(), "验证第"+count+"条返回BatchCode");
		
				couponbatch couponbatch = new couponbatchWapper()
						.selectBybatchCode(info.getBatchCode()).get(0);
				
				coupon coupon = new couponWapper().selectByBatchId(couponbatch.getBatchId()).get(0);
				
		
				Logger.verifyEquals(coupon.getBatchId(), info.getBatchId(),
						"验证第"+count+"条返回BatchId");
				

				Logger.verifyEquals(coupon.getBatchId(), couponbatch.getBatchId(),
						"验证第"+count+"条数据库表couponbatch与表coupon中batchId");
				
				Logger.verifyEquals(couponbatch.getMinOrderValue().stripTrailingZeros().toPlainString(),info.getMinOrderValue().stripTrailingZeros().toPlainString(),
						"验证第"+count+"条数据库表couponbatch与返回中的MinOrderValue");
				Logger.verifyEquals(couponbatch.getCouponValue().stripTrailingZeros().toPlainString(),info.getCouponValue().stripTrailingZeros().toPlainString(),
						"验证第"+count+"条数据库表couponbatch与返回中的CouponValue");
				
				SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				
				Logger.verifyEquals(formatter.format(couponbatch.getValidStart()),info.getValidStart().substring(0, 19).replace("T", " "),
						"验证第"+count+"条数据库表couponbatch与返回中的ValidStart");
				Logger.verifyEquals(formatter.format(couponbatch.getValidEnd()),info.getValidEnd().substring(0, 19).replace("T", " "),
						"验证第"+count+"条数据库表couponbatch与返回中的ValidEnd");
				
				Logger.verifyEquals(formatter.format(couponbatch.getAcquireStartTime()),info.getAcquireStartTime().substring(0, 19).replace("T", " "),
						"验证第"+count+"条数据库表couponbatch与返回中的AcquireStartTime");
				Logger.verifyEquals(formatter.format(couponbatch.getAcquireEndTime()),info.getAcquireEndTime().substring(0, 19).replace("T", " "),
						"验证第"+count+"条数据库表couponbatch与返回中的AcquireEndTime");
			/*	Logger.verifyEquals(couponbatch.getReceiveTimesPerUser(),info.getMaxReceiveCountPerUser(),
						"验证第"+count+"条数据库表couponbatch与返回中的MaxUseTimePerUser");*/
				
				Logger.verifyEquals(couponbatch.getCouponType(),info.getCouponType(),
						"验证第"+count+"条数据库表couponbatch与返回中的CouponType");
				Logger.verifyEquals(couponbatch.getCouponTypeBizValue(),info.getCouponTypeBizValue(),
						"验证第"+count+"条数据库表couponbatch与返回中的CouponTypeBizValue");
		
				Logger.verifyEquals(1, couponbatch.getReceivedCount(), "验证第"+count+"条数据库表couponbatch中iReceiveCount");
    		}
    	}
    	else
    		Logger.verifyNotNull(false, "验证没有可获取的数据");
    }
}